package com.example.ktapplication.activity
import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.example.ktapplication.R
import com.example.ktapplication.adapter.GuidePageAdapter
import com.example.ktapplication.bean.GuideBean

class GuidePageActivity : AppCompatActivity() {

    val mList: MutableList<GuideBean> = ArrayList()
    lateinit var ll_indicator: LinearLayout
    lateinit var viewPager:ViewPager2
    private var mImageList: MutableList<ImageView> = ArrayList()

    fun setStatusBarColor(
        activity: Activity,
        statusBarColor: Int = -1, //状态栏背景色
        isModifyStatusBarColor: Boolean //状态栏文字，是否更改颜色
    ) {
        val window = activity.window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        if (statusBarColor != -1)
            window.statusBarColor = activity.resources.getColor(statusBarColor)
        //由于状态栏图标及字体默认都是白色的，所以设置沉浸式状态栏为白色时需要修改状态栏字体颜色
        if (isModifyStatusBarColor)
            activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        else
            activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)
//        <color name="tr">#00000000</color>
        //透明化-修改状态栏背景色
        setStatusBarColor(this,R.color.tr, true)
        val bean1:GuideBean=GuideBean(R.mipmap.guide_1,"","")
        mList.add(bean1)
        val bean2:GuideBean=GuideBean(R.mipmap.guide_2,"","")
        mList.add(bean2)
        val bean3:GuideBean=GuideBean(R.mipmap.guide_3,"","")
        mList.add(bean3)
        val bean4:GuideBean=GuideBean(R.mipmap.guide_4,"","")
        mList.add(bean4)
        val bean5:GuideBean=GuideBean(R.mipmap.guide_5,"","")
        mList.add(bean5)
        initView()
        initEvent()
    }

    private fun initEvent() {
        viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                for (i in mImageList.indices) {
                    //选中的页面改变小圆点为选中状态，反之为未选中
                    val params = LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                        )
                    )
                    //为小圆点左右添加间距
                    params.leftMargin = 16
                    params.rightMargin = 16
                    if ((position % mImageList.size) == i) {
                        params.height = 24
                        params.width = 24
                        mImageList.get(i)
                            .setBackgroundResource(R.drawable.guide_indicator_select_share)
                        mImageList.get(i).setLayoutParams(params)
                    } else {
                        params.height = 24
                        params.width = 24
                        mImageList.get(i)
                            .setBackgroundResource(R.drawable.guide_indicator_unselected_share)
                        mImageList.get(i).setLayoutParams(params)
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })
    }

    fun initView(){
        ll_indicator = findViewById<LinearLayout>(R.id.ll_indicator)
        viewPager = findViewById<ViewPager2>(R.id.viewPager)
        viewPager.setOffscreenPageLimit(4);
        //透明化-允许控件延伸至导航栏 3
        viewPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        //初始化指示器
        initIndicator()
        val adapter=GuidePageAdapter()
        adapter.setData(mList,this)
        viewPager.adapter = adapter
    }

    private fun initIndicator() {
        for (i in mList.indices) {
            val imageView = ImageView(this)
            val params = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                )
            )
            //为小圆点左右添加间距
            params.leftMargin = 16
            params.rightMargin = 16
            if (i == 0) {
                //给小圆点一个默认大小
                params.height = 24
                params.width = 24
                imageView.setBackgroundResource(R.drawable.guide_indicator_select_share)
                imageView.layoutParams = params
            } else {
                //给小圆点一个默认大小
                params.height = 24
                params.width = 24
                imageView.setBackgroundResource(R.drawable.guide_indicator_unselected_share)
                imageView.layoutParams = params
            }
            ll_indicator.addView(imageView, params)
            mImageList.add(imageView)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}